 #include<bits/stdc++.h>
using namespace std;
int t,n;
int a[200005];
int c[200005];
int b[200005];
int d[1000005];
int main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    cin>>t;
    while(t--){
        cin>>n;
        memset(d,0,sizeof(d));
        memset(b,0,sizeof(b));
        for(int i=1;i<=n;i++){
            cin>>a[i];
            if(d[a[i]]){
                b[i]=d[a[i]];
            }
            d[a[i]]=i;
        }
        for(int i=2;i<=n;i++){
            c[i]=c[i-1];
            if(a[i]+c[b[i]]>c[i]&&b[i]){
                c[i]=c[b[i]]+a[i];
            }
        }
        cout<<c[n]<<endl;
    }
    fclose(stdin);
    fclose(stdout);
    return 0;
}
